/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import Model.User;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * To register a new user
 * @author Bryan
 */
public class RegisterServlet extends HttpServlet {

    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    /**
     * Insert a new user
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection con = DBConnector.connect();
        String  checkid = null;
        
        try{
       
        String userid = request.getParameter("username");
        String password = request.getParameter("password");
        String lastname = request.getParameter("lastname");
        String firstname = request.getParameter("firstname");
        String middlename = request.getParameter("middlename");
        String contact = request.getParameter("contact");
        String address = request.getParameter("address");
        String secretquestion1 = request.getParameter("secretquestion1");
        String answer1 = request.getParameter("answer1");
        String secretquestion2 = request.getParameter("secretquestion2");
        String answer2 = request.getParameter("answer2");
        int status = 0;
      
            if(checkid == null){
                PreparedStatement ps = con.prepareStatement
                        ("insert into quicklenddb.user(username, password,sec_quest_1,sec_ans_1,sec_quest_2,sec_ans_2,lastname,firstname,middlename,contact,address,status)"
                        + "values(?,md5(?),?,?,?,?,?,?,?,?,?,?)");
                
                ps.setString(1, userid);
                ps.setString(2, password);
                ps.setString(3, secretquestion1);
                ps.setString(4, answer1);
                ps.setString(5, secretquestion2);
                ps.setString(6, answer2);
                ps.setString(7, lastname);
                ps.setString(8, firstname);
                ps.setString(9, middlename);
                ps.setString(10, contact);
                ps.setString(11, address);
                ps.setInt(12, status);
                int checker = ps.executeUpdate();
                
                if(checker == 0){
                    response.sendRedirect("register.jsp");
                }
                else
                {
                    response.sendRedirect("registersuccess.jsp");
                }
            }
            else
            {
                response.sendRedirect("register.jsp");
            }
            
        }catch(SQLException e)
        {
            System.out.println(e); 
           
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void processRequest(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
}

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */

